/**
 * -----------------------------------------------------------------
 * 迭代器模式
 */

class Iterator {
  constructor(container) {
    this.list = container.list
    this.index = 0
  }
  next() {
    if (this.hasNext()) {
      return this.list[this.index++]
    }
    return null
  }
  hasNext() {
    if (this.index >= this.list.length) {
      return false
    }
    return true
  }
}

class Container {
  constructor(list) {
    this.list = list
  }
  getIterator() {
    return new Iterator(this)
  }
}

let container = new Container([0 ,2, 3, 4, 5])
let iterator = container.getIterator()
while(iterator.hasNext()) {
  console.log(iterator.next())
}


/**
 * -----------------------------------------------------------------
 * ES6 Iterator 原理 使用Symbol.iterator
 */

function each(data) {
  // 生成迭代器
  let iterator = data[Symbol.iterator]()

  let item = {done: false}
  while(!item.done) {
    item = iterator.next()
    if (!item.done) {
      console.log(item.value)
    }
  }
}

/**
 * -----------------------------------------------------------------
 * ES6 Iterator 简易版 for...of
 */

function each(data) {
  for(item of data) {
    console.log(item)
  }
}

// each(arr)
// each(nodeList)
// each(map)